﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

public class CheckAssetBundle
{

    public static void PrintAssetBundle2()
    {
        string[] names = AssetDatabase.GetAllAssetBundleNames();
        for (int i = 0, len = names.Length; i < len; i++)
        {
            string name = names[i];
            StringBuilder sb = new StringBuilder();
            string path = Path.Combine(Config.STREAMING_ASSETS_PATH, name);
            AssetBundle ab = LoadBundle(path);
            Object[] assets = ab.LoadAllAssets();
            sb.AppendLine(string.Format("{0}:{1}\ncount:{2}", i + 1, name, assets.Length));
            for (int j = 0, len2 = assets.Length; j < len2; j++)
            {
                string assetName = assets[j].name;
                sb.AppendLine(string.Format("  {0}:{1}", j + 1, assetName));
            }
            ab.Unload(true);
            Debug.Log(sb.ToString());
        }
    }

    // 检测AssetBundle交叉依赖
    public static void CheckCrossDependency()
    {
        StringBuilder builder = new StringBuilder();
        StringBuilder builder_dep = new StringBuilder();

        string[] names = AssetDatabase.GetAllAssetBundleNames();
        foreach (string name in names)
        {
            string[] dependencies = AssetDatabase.GetAssetBundleDependencies(name, true);
            foreach (string dependency in dependencies)
            {
                string[] dependencies2 = AssetDatabase.GetAssetBundleDependencies(dependency, true);
                foreach (string dependency2 in dependencies2)
                {
                    if (string.Equals(name, dependency2))
                    {
                        builder.AppendFormat("{0} <-> {1}\n", name, dependency);
                        for (int i = 0; i < dependencies.Length; i++)
                        {
                            Debug.Log(dependencies[i]);
                            builder_dep.Append(dependencies[i]);
                        }
                        Debug.Log("-----------------------------");
                        for (int i = 0; i < dependencies2.Length; i++)
                        {
                            Debug.Log(dependencies2[i]);
                            builder_dep.Append(dependencies2[i]);
                        }
                    }
                }
            }
        }
        string txt_dep = builder_dep.ToString();
        string txt = builder.ToString();
        if (!string.IsNullOrEmpty(txt))
        {
            Debug.LogFormat("以下ab出现交叉依赖:\n{0}", txt);
        }
        if (!string.IsNullOrEmpty(txt_dep))
        {
            Debug.LogFormat("以下ab的全部引用", txt_dep);
        }
    }

    static byte[] Unpack(byte[] bytes)
    {
        byte[] result = new byte[1];
        int size = LzmaHelper.Uncompress(bytes, ref result);
        if (size == 0)
        {
            return null;
        }
        return result;
    }

    static AssetBundle LoadBundle(string path)
    {
        byte[] temp = FileHelper.ReadBytesFromFile(path);
        temp = Unpack(temp);
        AssetBundle bundle = AssetBundle.LoadFromMemory(temp);
        return bundle;
    }

    public static void PrintAssetBundle()
    {
        string path = Path.Combine(Config.STREAMING_ASSETS_PATH, Config.MANIFEST_NAME);
        AssetBundle bundle = LoadBundle(path);
        AssetBundleManifest manifest = bundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
        string[] names = manifest.GetAllAssetBundles();
        for (int i = 0, len = names.Length; i < len; i++)
        {
            string name = names[i];
            Debug.Log(string.Format("{0}:{1}", i + 1, name));
        }
        bundle.Unload(true);
    }
}